/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "WorkbenchWgt.h"

#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QComboBox>

#include "SqlServer.h"
#include "CreateSeqDatabaseDlg.h"

/**
**/
WorkbenchWgt::WorkbenchWgt( QWidget *parent ) : QWidget( parent ), m_server( NULL )
{
	initLayout();
}

/**
**/
WorkbenchWgt::~WorkbenchWgt()
{
}

/**
**/
void WorkbenchWgt::setServer( Bio::SqlServer *server )
{
	m_server = server;
	updateSequenceDB();
}

/**
**/
void WorkbenchWgt::setSeqDB( int which )
{
	if( which == 0 ){
		// None
	}
	else if( which == m_seqDBCombo->count() - 1 ){
		CreateSeqDatabaseDlg	dlg( parentWidget(), m_server );

		if( dlg.exec() == QDialog::Accepted ){
		}
	}
	else {
		// Select the given DB
	}
}

/**
**/
void WorkbenchWgt::initLayout()
{
	int			row = 0;
	int			col = 0;
	QLabel		*lbl;
	QGridLayout	*layout = new QGridLayout;

	setLayout( layout );

	lbl = new QLabel( tr( "Sequence Database:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_seqDBCombo = new QComboBox( this );
	layout->addWidget( m_seqDBCombo, row, col++ );
	connect( m_seqDBCombo, SIGNAL(activated(int)), this, SLOT(setSeqDB(int)) );
	updateSequenceDB();	

	row++;
	col = 0;

	layout->setColumnStretch( 1, 100 );
	layout->setRowStretch( row, 100 );
}

/**
**/
void WorkbenchWgt::updateSequenceDB()
{
	m_seqDBCombo->clear();
	m_seqDBCombo->addItem( tr( "None" ) );

	if( m_server ){
		QStringList	seqDB = m_server->getSeqDatabaseNames();

		if( !seqDB.isEmpty() )
			m_seqDBCombo->addItems( seqDB );

		m_seqDBCombo->addItem( tr( "Create database..." ) );
	}
}